# vim: ts=4:sw=4:syntax=perl

use gri_secmgr qw(get_cookie_value redirect_header);
use gri_user_alert_config;
use xml_helper;

sub mm_form_modify_report_process {
my $_aa=new xml_helper();

	my $url_prefix=$::GRI_FRONTEND->get_url_prefix();
	my $str="";

	my $rep_name=$::PARAM{name};
	my $rep_filename=$::PARAM{filename};
	my $rep_title=$::PARAM{title};
	my $rep_directory=$::PARAM{directory};
	my $rep_host=$::PARAM{host};
	my $rep_displaytext=$::PARAM{displaytext};
	my $rep_type=$::PARAM{type};
	my $rep_attributes=$::PARAM{attributes};
	my $rep_convert_type=$::PARAM{convert_type};
	my $rep_convert_desc=$::PARAM{convert_desc};
	my $rep_convert_args=$::PARAM{convert_args};
	my $rep_desc=$::PARAM{desc};
	my $rep_return_to="";
	if(exists($::PARAM{return_to})) {
		$rep_return_to=hex_to_string($::PARAM{return_to});
	}
	if($rep_convert_type eq "Not Required") {
		$rep_convert_type="";
		$rep_convert_desc="";
		$rep_convert_args="";
	}

	#########################################################
	# Check to ensure we DO already have a report of this	#
	# name and raise an error if we do.			#
	#########################################################
	{
		my $x=$::GRI_FRONTEND->get_global_directory("reports");
		if(! -f "$x/$rep_filename") {
			my $serror="Error Updating Existing Report";
			my $error="Specified report filename does not exists";

			my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error);
			if(!exists($::PARAM{lp})) {
				# We need to encode the current data for repopulation into the form!
				my $s="";
				$s.="rep_name=".string_to_hex($rep_name);
				$s.="::rep_filename=".string_to_hex($rep_filename);
				$s.="::rep_title=".string_to_hex($rep_title);
				$s.="::rep_directory=".string_to_hex($rep_directory);
				$s.="::rep_host=".string_to_hex($rep_host);
				$s.="::rep_displaytext=".string_to_hex($rep_displaytext);
				$s.="::rep_type=".string_to_hex($rep_type);
				$s.="::rep_attributes=".string_to_hex($rep_attributes);
				$s.="::rep_convert_type=".string_to_hex($rep_convert_type);
				$s.="::rep_convert_desc=".string_to_hex($rep_convert_desc);
				$s.="::rep_convert_args=".string_to_hex($rep_convert_args);
				$s.="::rep_desc=".string_to_hex($rep_desc);
				$s.="::rep_return_to=".string_to_hex($rep_return_to);
				$link.=",previouspage=form_modify_report::$s";
			}
			redirect_header(path => $link);
			exit;
		}
	}

	#################################################################
	# First thing we do is to validate that we can communicate	#
	# with the host specified and if not we bump around to the	#
	# form via an error window.					#
	#################################################################

	my ($rc,$err)=$::GRI_FRONTEND->get_host_ctime($rep_host);
	if(!defined($rc)) {
		my $serror="Error Adding New Report";
		my $error="Specified host is not responding.";

		my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error);
		if(!exists($::PARAM{lp})) {
			# We need to encode the current data for repopulation into the form!
			my $s="";
			$s.="rep_name=".string_to_hex($rep_name);
			$s.="::rep_filename=".string_to_hex($rep_filename);
			$s.="::rep_title=".string_to_hex($rep_title);
			$s.="::rep_directory=".string_to_hex($rep_directory);
			$s.="::rep_host=".string_to_hex($rep_host);
			$s.="::rep_displaytext=".string_to_hex($rep_displaytext);
			$s.="::rep_type=".string_to_hex($rep_type);
			$s.="::rep_attributes=".string_to_hex($rep_attributes);
			$s.="::rep_convert_type=".string_to_hex($rep_convert_type);
			$s.="::rep_convert_desc=".string_to_hex($rep_convert_desc);
			$s.="::rep_convert_args=".string_to_hex($rep_convert_args);
			$s.="::rep_desc=".string_to_hex($rep_desc);
			$s.="::rep_return_to=".string_to_hex($rep_return_to);
			$link.=",previouspage=form_modify_report::$s";
		}
		redirect_header(path => $link);
		exit;
	}
	$rc=$::GRI_FRONTEND->query_backend($rep_host,"DIR_EXISTS DIR=$rep_directory");
	if(!defined($rc) || $rc =~ /^ERROR/) {
		my $serror="Error Adding New Report";
		my $error="Specified backend is not responding to 'DIR_EXISTS' query.";

		my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error);
		if(!exists($::PARAM{lp})) {
			# We need to encode the current data for repopulation into the form!
			my $s="";
			$s.="rep_name=".string_to_hex($rep_name);
			$s.="::rep_filename=".string_to_hex($rep_filename);
			$s.="::rep_title=".string_to_hex($rep_title);
			$s.="::rep_directory=".string_to_hex($rep_directory);
			$s.="::rep_host=".string_to_hex($rep_host);
			$s.="::rep_displaytext=".string_to_hex($rep_displaytext);
			$s.="::rep_type=".string_to_hex($rep_type);
			$s.="::rep_attributes=".string_to_hex($rep_attributes);
			$s.="::rep_convert_type=".string_to_hex($rep_convert_type);
			$s.="::rep_convert_desc=".string_to_hex($rep_convert_desc);
			$s.="::rep_convert_args=".string_to_hex($rep_convert_args);
			$s.="::rep_desc=".string_to_hex($rep_desc);
			$s.="::rep_return_to=".string_to_hex($rep_return_to);
			$link.=",previouspage=form_modify_report::$s";
		}
		redirect_header(path => $link);
		exit;
	}

	chomp $rc;
	if($rc eq "NO") {
		my $serror="Error Adding New Report";
		my $error="Specified directory does not exist.";

		my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error);
		if(!exists($::PARAM{lp})) {
			# We need to encode the current data for repopulation into the form!
			my $s="";
			$s.="rep_name=".string_to_hex($rep_name);
			$s.="::rep_filename=".string_to_hex($rep_filename);
			$s.="::rep_title=".string_to_hex($rep_title);
			$s.="::rep_directory=".string_to_hex($rep_directory);
			$s.="::rep_host=".string_to_hex($rep_host);
			$s.="::rep_displaytext=".string_to_hex($rep_displaytext);
			$s.="::rep_type=".string_to_hex($rep_type);
			$s.="::rep_attributes=".string_to_hex($rep_attributes);
			$s.="::rep_convert_type=".string_to_hex($rep_convert_type);
			$s.="::rep_convert_desc=".string_to_hex($rep_convert_desc);
			$s.="::rep_convert_args=".string_to_hex($rep_convert_args);
			$s.="::rep_desc=".string_to_hex($rep_desc);
			$s.="::rep_return_to=".string_to_hex($rep_return_to);
			$link.=",previouspage=form_modify_report::$s";
		}
		redirect_header(path => $link);
		exit;
	}

	#########################################################################
	# We would now check at least a single report exists, and if not	#
	# indicate an error has occured.					#
	#########################################################################

	$rc=$::GRI_FRONTEND->query_backend($rep_host,"GET_REPORT_LIST DIR=$rep_directory NAME=$rep_name");
	my $serror=undef;
	my $error=undef;
	if(!defined($rc) || $rc =~ /^ERROR/) {
		$serror="Error Adding New Report";
		$error="Specified backend is not responding to 'GET_REPORT_LIST' query.";
	}
	chomp($rc) if defined($rc);
	print STDERR "rc=$rc\n";
	if(defined($rc) && $rc eq "0") {
		$serror="Error Adding New Report";
		$error="No reports matching the specified name in the directory could be found.";
	}
	if(defined($serror)) {

		my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error);
		if(!exists($::PARAM{lp})) {
			# We need to encode the current data for repopulation into the form!
			my $s="";
			$s.="rep_name=".string_to_hex($rep_name);
			$s.="::rep_filename=".string_to_hex($rep_filename);
			$s.="::rep_title=".string_to_hex($rep_title);
			$s.="::rep_directory=".string_to_hex($rep_directory);
			$s.="::rep_host=".string_to_hex($rep_host);
			$s.="::rep_displaytext=".string_to_hex($rep_displaytext);
			$s.="::rep_type=".string_to_hex($rep_type);
			$s.="::rep_attributes=".string_to_hex($rep_attributes);
			$s.="::rep_convert_type=".string_to_hex($rep_convert_type);
			$s.="::rep_convert_desc=".string_to_hex($rep_convert_desc);
			$s.="::rep_convert_args=".string_to_hex($rep_convert_args);
			$s.="::rep_desc=".string_to_hex($rep_desc);
			$s.="::rep_return_to=".string_to_hex($rep_return_to);
			$link.=",previouspage=form_modify_report::$s";
		}
		redirect_header(path => $link);
		exit;
	}

	# Everything is good - so replace the file...
WRITEFILE: {
		$serror=$error=undef;
		my $x=$::GRI_FRONTEND->get_global_directory("reports");
		if(!open($fd,">$x/$rep_filename.tmp")) {
			$serror="Error Adding New Report";
			$error="Unable to write to file '$x/$rep_filename': $!";
			last WRITEFILE;
		}
		$serror="Error Adding New Report";
		$error="Unable to write to file '$x/$rep_filename.tmp': $!";
		if(! print $fd "<?xml version=\"1.0\" standalone=\"yes\"?>
<report>
	<title>" . $_aa->string_to_xml($rep_title) . "</title>
	<host>" . $_aa->string_to_xml($rep_host) . "</host>
	<directory>" . $_aa->string_to_xml($rep_directory) . "</directory>
	<name>" . $_aa->string_to_xml($rep_name) . "</name>
	<type>" . $_aa->string_to_xml($rep_type) . "</type>
	<dynamic_convert_type>" . $_aa->string_to_xml($rep_convert_type) . "</dynamic_convert_type>
	<dynamic_convert_desc>" . $_aa->string_to_xml($rep_convert_desc) . "</dynamic_convert_desc>
	<dynamic_convert_args>" . $_aa->string_to_xml($rep_convert_args) . "</dynamic_convert_args>
	<attributes>" . $_aa->string_to_xml($rep_attributes) . "</attributes>
") {
			last WRITEFILE;
		}
		if(defined($rep_displaytext) && length($rep_displaytext)) {
			if(! print $fd "	<display_name>" . $_aa->string_to_xml($rep_displaytext) . "</display_name>\n") {
				last WRITEFILE;
			}
		}
		if(defined($rep_desc) && length($rep_desc)) {
			my $rr=encode_base64($rep_desc,"");
			if(! print $fd "	<desc>$rr</desc>\n") {
				last WRITEFILE;
			}
		}
		if(! print $fd "</report>\n") {
			last WRITEFILE;
		}
		close($fd);
		$serror=$error=undef;
		rename("$x/$rep_filename","$x/$rep_filename.old");
		rename("$x/$rep_filename.tmp","$x/$rep_filename");
		unlink("$x/$rep_filename.old");
	}

	if(defined($serror)) {

		my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error);
		if(!exists($::PARAM{lp})) {
			# We need to encode the current data for repopulation into the form!
			my $s="";
			$s.="rep_name=".string_to_hex($rep_name);
			$s.="::rep_filename=".string_to_hex($rep_filename);
			$s.="::rep_title=".string_to_hex($rep_title);
			$s.="::rep_directory=".string_to_hex($rep_directory);
			$s.="::rep_host=".string_to_hex($rep_host);
			$s.="::rep_displaytext=".string_to_hex($rep_displaytext);
			$s.="::rep_type=".string_to_hex($rep_type);
			$s.="::rep_attributes=".string_to_hex($rep_attributes);
			$s.="::rep_convert_type=".string_to_hex($rep_convert_type);
			$s.="::rep_convert_desc=".string_to_hex($rep_convert_desc);
			$s.="::rep_convert_args=".string_to_hex($rep_convert_args);
			$s.="::rep_desc=".string_to_hex($rep_desc);
			$s.="::rep_return_to=".string_to_hex($rep_return_to);
			$link.=",previouspage=form_modify_report::$s";
		}
		redirect_header(path => $link);
		exit;
	}

	if($rep_return_to) {
		my $link="$url_prefix$rep_return_to";
		redirect_header(path => $link);
		exit;
	}
	my $link="$url_prefix$::ENV{SCRIPT_NAME}/admin_page";
	redirect_header(path => $link);
	exit;
}

